package com.ruoyi.mateWarehousing.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheckModel;
import com.ruoyi.mateCheck.domain.MateTest;
import com.ruoyi.mateCheck.domain.MateUserDevice;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.mateRequistion.domain.MateRequistionModel;
import com.ruoyi.mateRequistion.mapper.MateRequistionMapper;
import com.ruoyi.mateWarehousing.domain.Inventory;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.mapper.MateWarehousingMapper;
import com.ruoyi.mateWarehousing.service.MateWarehousingService;
import com.ruoyi.material.domain.MateProductModel;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateWarehousingServiceImpl implements MateWarehousingService {

    @Autowired
    MateWarehousingMapper mateWarehousingMapper;

    @Autowired
    WarehousingRecordsMapper warehousingRecordsMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Autowired
    MateDeviceMapper mateDeviceMapper;

    @Autowired
    MateProductMapper mateProductMapper;

    @Autowired
    MateRequistionMapper mateRequistionMapper;

    @Override
    public List<MaterialProduct> selectProductToWar(MaterialProduct materialProduct) {
        return mateWarehousingMapper.selectProductToWar(materialProduct);
    }

    @Override
    public List<MateWarehousing> selectWarehousingDetail(MateWarehousing mateWarehousing) {
        return mateWarehousingMapper.selectWarehousingDetail(mateWarehousing);
    }

    @Override
    public List<WarehousingRecords> selectWarehousingRecords(WarehousingRecords warehousingRecords) {
        warehousingRecords.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        return warehousingRecordsMapper.selectMateWarehousingRecords(warehousingRecords);
    }

    @Override
    public List<MateCheckModel> selectMateCheck(MateWarehousing mateWarehousing) {
        return mateCheckMapper.selectMateWarehousingCheck(mateWarehousing);
    }

    @Override
    public List<MateUserDevice> selectmateUserDevices(MateWarehousing mateWarehousing) {
        return mateDeviceMapper.selectmateUserDevices(mateWarehousing);
    }

    @Override
    public List<MateWarehousing> mateProductList(Map<String, Object> resMap) {
        return mateWarehousingMapper.mateProductList(resMap);
    }

    @Override
    public List<MateWarehousing> selectMateWarehousing(Map<String, Object> resMap) {
        return mateWarehousingMapper.selectMateWarehousingList(resMap);
    }

    @Override
    public List<MateRequistionModel> selectTendency() {
        Long deptId = SecurityUtils.getDeptId();
        List<MateWarehousing> mateWarehousings = mateWarehousingMapper.selectTendency(deptId);
        LocalDate date = LocalDate.now();
        MaterialProduct materialProduct;

        List<MateRequistionModel> requistionModelList = new ArrayList<>();
        for (MateWarehousing mateWarehousing : mateWarehousings) {
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("companyId", deptId);
            resMap.put("mateproductId", mateWarehousing.getMateproductId());
            materialProduct = mateProductMapper.selectMaterialProductByMap(resMap);

            MateRequistionModel requistionModel = new MateRequistionModel();
            MateRequistionModel requistionModel1 = new MateRequistionModel();
            MateRequistionModel requistionModel2 = new MateRequistionModel();

            requistionModel.setBeginTime(String.valueOf(date.plusDays(-materialProduct.getSelectCycle())));
            requistionModel.setDeptId(deptId);
            requistionModel.setEndTime(date.toString());
            requistionModel2.setDeptId(deptId);
            requistionModel2.setMateproductId(materialProduct.getMateproductId());

            requistionModel1.setBeginTime(String.valueOf(date.plusDays(-materialProduct.getSelectCycle()).minusYears(1)));
            requistionModel1.setEndTime(String.valueOf(date.minusYears(1)));
            requistionModel.setMateproductId(mateWarehousing.getMateproductId());
            MateRequistionModel model = mateRequistionMapper.selectRequistionModel(requistionModel);
            MateRequistionModel model1 = mateRequistionMapper.selectRequistionModel(requistionModel1);
            MateRequistionModel requistionModel3 = new MateRequistionModel();

            if (model != null && model1 != null) {

                requistionModel2.setBeginTime(requistionModel1.getEndTime());
                requistionModel2.setEndTime(String.valueOf(date.minusYears(1).plusDays(materialProduct.getPurchaseCycle())));
                int sumNum = mateRequistionMapper.selectSumAllnum(requistionModel2);

                requistionModel3.setBeginTime(String.valueOf(date.minusYears(1).plusDays(materialProduct.getPurchaseCycle())));
                requistionModel3.setEndTime(String.valueOf(date.minusYears(1).plusDays(materialProduct.getPurchaseCycle() + materialProduct.getPurchaseTime())));
                requistionModel3.setDeptId(deptId);
                requistionModel3.setMateproductId(materialProduct.getMateproductId());

                int historySumNum = mateRequistionMapper.selectSumAllnum(requistionModel3);

                Double a = Double.valueOf(model.getRealityNum());
                Double b = Double.valueOf(model1.getRealityNum());

                requistionModel.setMateProductName(model.getMateProductName());
                requistionModel.setMateProductType(model.getMateProductType());
                requistionModel.setMateproductId(model.getMateproductId());
                requistionModel.setPurchaseCycle(materialProduct.getPurchaseCycle());
                requistionModel.setWarehousingNum(Long.valueOf(mateWarehousing.getWarehousingNum()));
                requistionModel.setHistoryNum(Integer.parseInt(requistionModel1.getRealityNum()));
                requistionModel.setTendency(round((a - b) / b, 2));
                requistionModel.setPurchaseUnit(materialProduct.getPurchaseUnit());
                requistionModel.setRealityNum(requistionModel.getRealityNum());
                requistionModel.setExpectNum((int) (sumNum * (1 + requistionModel.getTendency())));
                requistionModel.setPurchaseNum((int) (historySumNum * (1 + requistionModel.getTendency())));
                requistionModel.setSelectCycle(materialProduct.getSelectCycle());
                requistionModelList.add(requistionModel);

            }

        }
        return requistionModelList;
    }

    public static double round(double value, int places) {
        return BigDecimal.valueOf(value)
                .setScale(places, RoundingMode.HALF_UP)
                .doubleValue();
    }

    @Override
    public List<Inventory> selectProductToWar_new(Inventory inventory) {
        // 设置数据范围
        inventory.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        return mateWarehousingMapper.selectProductToWar_new(inventory);
    }
}
